package com.alibaba.algo.components.permission.interceptor;

import com.alibaba.algo.components.permission.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: glorysongglory
 * Date: 2020/11/5
 * Time: 16:18
 */
@Component
public class JwtTokenInterceptor implements HandlerInterceptor {

    @Autowired
    TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        String token = tokenService.getTokenFromHttpServletRequest(httpServletRequest);
        //校验token，如果有异常直接抛出
        tokenService.checkToken(token);
        //刷新token expire
//        tokenService.refreshToken(token, 30l, TimeUnit.MINUTES);

        return true;
    }
}
